<template>
  <div class="goods-list-wrapper">
    <div class="list-header van-hairline--top-bottom">
      <div :class="{ active: type == 'all' }" @click="changeType('all')">
        综合
      </div>
      <div :class="{ active: type == 'sale' }" @click="changeType('sale')">
        销量
      </div>
      <div
        class="price"
        :class="{
          'price-up': type == 'price-up',
          'price-down': type == 'price-down',
        }"
        @click="changeType('price')"
      >
        价格
      </div>
    </div>
    <div class="list-content">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
          :immediate-check="false"
        >
          <GoodsCard
            v-for="(item, i) in goodsList"
            :key="i"
            v-bind="item"
            :num="counterMap[item.id]"
          />
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from 'vuex';
import GoodsCard from './GoodsCard.vue';

export default {
  components: {
    GoodsCard,
  },
  data() {
    return {
      type: 'price-up',
      list: [],
      loading: false,
      finished: false,
      isLoading: false,
      page: 1,
    };
  },
  computed: {
    ...mapState(['goodsList', 'counterMap']),
  },
  methods: {
    ...mapMutations(['resetGoodsList']),
    ...mapActions(['setGoodsList']),
    changeType(type) {
      if (type === 'all') {
        this.type = 'all';
      } else if (type === 'sale') {
        this.type = 'sale';
      } else if (this.type === 'price-up') {
        this.type = 'price-down';
      } else {
        this.type = 'price-up';
      }
      this.onRefresh();
    },
    async onLoad() {
      this.page += 1;
      this.loading = true;
      const result = await this.setGoodsList({
        page: this.page,
        sortType: this.type,
      });
      if (result) {
        this.loading = false;
      } else {
        this.finished = true;
      }
    },
    onRefresh() {
      // 清空列表数据
      this.isLoading = true;
      this.finished = false;
      this.loading = false;
      this.page = 1;
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.resetGoodsList();
      this.setGoodsList({ sortType: this.type, page: 1 });
      this.isLoading = false;
    },
  },
};
</script>

<style lang="less" scoped>
.goods-list-wrapper {
  .list-header {
    width: 296px;
    position: relative;
    top: -13px;
    left: 79px;
    padding: 0 8px;
    box-sizing: border-box;
    display: flex;
    justify-content: flex-end;
    > div {
      width: 50px;
      height: 25px;
      text-align: center;
      line-height: 25px;
      color: #cecece;
      position: relative;
    }
    .price::after {
      content: "";
      display: block;
      border: 4px solid transparent;
      border-bottom-color: #aaa;
      position: absolute;
      right: 0;
      top: 4px;
    }
    .price::before {
      content: "";
      display: block;
      border: 4px solid transparent;
      border-top-color: #aaa;
      position: absolute;
      right: 0;
      bottom: 4px;
    }
    .active,
    .price-up,
    .price-down {
      color: #ff1a90;
      font-weight: bold;
    }
    .price-up::after {
      border-bottom-color: #ff1a90;
    }
    .price-down::before {
      border-top-color: #ff1a90;
    }
  }
  .list-content {
    width: 296px;
    position: fixed;
    top: 170px;
    right: 0;
    bottom: 50px;
    overflow: auto;
  }
}
.van-pull-refresh {
  overflow: unset !important;
}
</style>
